package com.example.zg3_park.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.day05rk.view.ViewAdapter
import com.example.day05rk.view.ViewModel
import com.example.lib_base.entity.ViewEntity
import com.example.lib_base.entity.ViewEntityItem
import com.example.lib_base.mvvm.BaseActivity
import com.example.zg3_park.R
import com.example.zg3_park.databinding.ActivityViewBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = "/app/ViewActivity")
class ViewActivity : BaseActivity<ActivityViewBinding,ViewModel>() {
    private val viewAdapter by lazy { ViewAdapter() }

    override fun intiView() {
        binding.rvView.apply {
            adapter = viewAdapter
            layoutManager = LinearLayoutManager(this@ViewActivity)
        }

        //搜索
        binding.sv.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                lifecycleScope.launch {
                    vm.getView(query!!,"搜索").collect{
                        viewAdapter.submitData(it)
                    }
                    vm.getView("","类型").collect{
                        viewAdapter.submitData(it)
                    }
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }

        })
    }

    override fun intiData() {
        lifecycleScope.launch {
            vm.getView("","类型").collect{
                viewAdapter.submitData(it)
            }
        }

        vm.searchViewData.observe(this) {

        }
    }
}